﻿using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Abp.Authorization;
using Abp.Authorization.Roles;
using Abp.Authorization.Users;
using Abp.MultiTenancy;
using WebApp.Authorization;
using WebApp.Authorization.Roles;
using WebApp.Authorization.Users;
using System;
using WebApp.Navigations;

namespace WebApp.EntityFrameworkCore.Seed.uiMenu
{
  
    public class UiMenuBuilder
    {
        private readonly WebAppDbContext _context;
        private readonly Guid _tenantId;

        public UiMenuBuilder(WebAppDbContext context, Guid tenantId)
        {
            _context = context;
            _tenantId = tenantId;
        }

        public void Create()
        {
            CreateUiMenus();
        }

        private void CreateUiMenus()
        {
            // Admin 模块

            var baseUiMenu = _context.UiMenus.IgnoreQueryFilters().FirstOrDefault(r => r.Name == "setting" );
            if (baseUiMenu == null)
            {
                UiMenu uiM = new UiMenu();
                uiM.Name = "setting";
                uiM.DisplayName = "uiMenu.setting";
                uiM.PermissionKey = "Pages.Setting";
                uiM.Path = "/setting";
                uiM.Icon = "md-settings";
                uiM.Pid = Guid.Empty;
                uiM.Order =1;
                uiM.IsEnable =true;
                uiM.Remark = "基础配置";
                var  reUim = _context.UiMenus.Add(uiM).Entity;

                UiMenu uiM1 = new UiMenu();
                uiM1.Name = "user";
                uiM1.DisplayName = "uiMenu.user";
                uiM1.PermissionKey = "Pages.Users";
                uiM1.Path = "/setting/user/user";
                uiM1.Icon = "md-contact";
                uiM1.Pid = uiM.Id;
                uiM1.Order = 1;
                uiM1.IsEnable = true;
                uiM1.Remark = "用户";

                UiMenu uiM2 = new UiMenu();
                uiM2.Name = "role";
                uiM2.DisplayName = "uiMenu.role";
                uiM2.PermissionKey = "Pages.Roles";
                uiM2.Path = "/setting/role/role";
                uiM2.Icon = "md-people";
                uiM2.Pid = uiM.Id;
                uiM2.Order = 1;
                uiM2.IsEnable = true;
                uiM2.Remark = "角色";

                UiMenu uiM3 = new UiMenu();
                uiM3.Name = "uiMenus";
                uiM3.DisplayName = "uiMenu.uiMenu";
                uiM3.PermissionKey = "Pages.UiMenu";
                uiM3.Path = "/setting/uiMenu/uiMenu";
                uiM3.Icon = "md-git-network";
                uiM3.Pid = uiM.Id;
                uiM3.Order = 1;
                uiM3.IsEnable = true;
                uiM3.Remark = "模块";

                UiMenu uiM4 = new UiMenu();
                uiM4.Name = "company";
                uiM4.DisplayName = "uiMenu.company";
                uiM4.PermissionKey = "Pages.Company";
                uiM4.Path = "/setting/company/company";
                uiM4.Icon = "md-git-network";
                uiM4.Pid = uiM.Id;
                uiM4.Order = 1;
                uiM4.IsEnable = true;
                uiM4.Remark = "公司管理";

                var reUim1 = _context.UiMenus.Add(uiM1).Entity;
                var reUim2 = _context.UiMenus.Add(uiM2).Entity;
                var reUim3 = _context.UiMenus.Add(uiM3).Entity;
                var reUim4 = _context.UiMenus.Add(uiM4).Entity;
                #region //添加按钮元素 用户
                //用户
                MenuElement Umele1 =new  MenuElement();
                Umele1.Name = "Pages.Users.AssignRole";
                Umele1.DisplayName = "Pages.Users.AssignRole";
                Umele1.PermissionKey = "Pages.Users.AssignRole";
                Umele1.Icon = "#";
                Umele1.CssStyle = "#";
                Umele1.UiMenuId = reUim1.Id;
                Umele1.IsEnable = true;

                MenuElement Umele2 = new MenuElement();
                Umele2.Name = "Pages.Users.Create";
                Umele2.DisplayName = "Pages.Users.Create";
                Umele2.PermissionKey = "Pages.Users.Create";
                Umele2.Icon = "#";
                Umele2.CssStyle = "#";
                Umele2.UiMenuId = reUim1.Id;
                Umele2.IsEnable = true;

                MenuElement Umele3 = new MenuElement();
                Umele3.Name = "Pages.Users.Delete";
                Umele3.DisplayName = "Pages.Users.Delete";
                Umele3.PermissionKey = "Pages.Users.Delete";
                Umele3.Icon = "#";
                Umele3.CssStyle = "#";
                Umele3.UiMenuId = reUim1.Id;
                Umele3.IsEnable = true;

                MenuElement Umele4 = new MenuElement();
                Umele4.Name = "Pages.Users.Edit";
                Umele4.DisplayName = "Pages.Users.Edit";
                Umele4.PermissionKey = "Pages.Users.Edit";
                Umele4.Icon = "#";
                Umele4.CssStyle = "#";
                Umele4.UiMenuId = reUim1.Id;
                Umele4.IsEnable = true;


                _context.MenuElements.Add(Umele1);
                _context.MenuElements.Add(Umele2);
                _context.MenuElements.Add(Umele3);
                _context.MenuElements.Add(Umele4);
                #endregion
                #region //添加按钮元素 角色
                //角色
                MenuElement Rmele1 = new MenuElement();
                Rmele1.Name = "Pages.Roles.AssignUiMenu";
                Rmele1.DisplayName = "Pages.Roles.AssignUiMenu";
                Rmele1.PermissionKey = "Pages.Roles.AssignUiMenu";
                Rmele1.Icon = "#";
                Rmele1.CssStyle = "#";
                Rmele1.UiMenuId = reUim2.Id;
                Rmele1.IsEnable = true;
                MenuElement Rmele2 = new MenuElement();
                Rmele2.Name = "Pages.Roles.Create";
                Rmele2.DisplayName = "Pages.Roles.Create";
                Rmele2.PermissionKey = "Pages.Roles.Create";
                Rmele2.Icon = "#";
                Rmele2.CssStyle = "#";
                Rmele2.UiMenuId = reUim2.Id;
                Rmele2.IsEnable = true;
                MenuElement Rmele3 = new MenuElement();
                Rmele3.Name = "Pages.Roles.Delete";
                Rmele3.DisplayName = "Pages.Roles.Delete";
                Rmele3.PermissionKey = "Pages.Roles.Delete";
                Rmele3.Icon = "#";
                Rmele3.CssStyle = "#";
                Rmele3.UiMenuId = reUim2.Id;
                Rmele3.IsEnable = true;
                MenuElement Rmele4 = new MenuElement();
                Rmele4.Name = "Pages.Roles.Edit";
                Rmele4.DisplayName = "Pages.Roles.Edit";
                Rmele4.PermissionKey = "Pages.Roles.Edit";
                Rmele4.Icon = "#";
                Rmele4.CssStyle = "#";
                Rmele4.UiMenuId = reUim2.Id;
                Rmele4.IsEnable = true;
                _context.MenuElements.Add(Rmele1);
                _context.MenuElements.Add(Rmele2);
                _context.MenuElements.Add(Rmele3);
                _context.MenuElements.Add(Rmele4);
                #endregion
                #region //添加按钮元素 模块
                //模块
                MenuElement Mmele1 = new MenuElement();
                Mmele1.Name = "Pages.MenuElement.Create";
                Mmele1.DisplayName = "Pages.MenuElement.Create";
                Mmele1.PermissionKey = "Pages.MenuElement.Create";
                Mmele1.Icon = "#";
                Mmele1.CssStyle = "#";
                Mmele1.UiMenuId = reUim3.Id;
                Mmele1.IsEnable = true;
                MenuElement Mmele2 = new MenuElement();
                Mmele2.Name = "Pages.MenuElement.Delete";
                Mmele2.DisplayName = "Pages.MenuElement.Delete";
                Mmele2.PermissionKey = "Pages.MenuElement.Delete";
                Mmele2.Icon = "#";
                Mmele2.CssStyle = "#";
                Mmele2.UiMenuId = reUim3.Id;
                Mmele2.IsEnable = true;
                MenuElement Mmele3 = new MenuElement();
                Mmele3.Name = "Pages.MenuElement.Edit";
                Mmele3.DisplayName = "Pages.MenuElement.Edit";
                Mmele3.PermissionKey = "Pages.MenuElement.Edit";
                Mmele3.Icon = "#";
                Mmele3.CssStyle = "#";
                Mmele3.UiMenuId = reUim3.Id;
                Mmele3.IsEnable = true;


                _context.MenuElements.Add(Mmele1);
                _context.MenuElements.Add(Mmele2);
                _context.MenuElements.Add(Mmele3);
                #endregion
                #region //添加按钮元素 公司
                //模块
                MenuElement Cmele1 = new MenuElement();
                Cmele1.Name = "Pages.Company.Create";
                Cmele1.DisplayName = "Pages.Company.Create";
                Cmele1.PermissionKey = "Pages.Company.Create";
                Cmele1.Icon = "#";
                Cmele1.CssStyle = "#";
                Cmele1.UiMenuId = reUim4.Id;
                Cmele1.IsEnable = true;
                MenuElement Cmele2 = new MenuElement();
                Cmele2.Name = "Pages.Company.Delete";
                Cmele2.DisplayName = "Pages.Company.Delete";
                Cmele2.PermissionKey = "Pages.Company.Delete";
                Cmele2.Icon = "#";
                Cmele2.CssStyle = "#";
                Cmele2.UiMenuId = reUim4.Id;
                Cmele2.IsEnable = true;
                MenuElement Cmele3 = new MenuElement();
                Cmele3.Name = "Pages.Company.Edit";
                Cmele3.DisplayName = "Pages.Company.Edit";
                Cmele3.PermissionKey = "Pages.Company.Edit";
                Cmele3.Icon = "#";
                Cmele3.CssStyle = "#";
                Cmele3.UiMenuId = reUim4.Id;
                Cmele3.IsEnable = true;


                _context.MenuElements.Add(Cmele1);
                _context.MenuElements.Add(Cmele2);
                _context.MenuElements.Add(Cmele3);
           
                #endregion
                _context.SaveChanges();
            }

            //
      
        }
    }
}
